<?php
namespace app\common\logic;
use think\Db;
use think\Exception;

class PkPayment
{
    private static $_instance;
    
    private function __construct() {}
    
    public static function getInstance()
    {
        if(!(self::$_instance instanceof self)) {
            self::$_instance = new self;
        }
        return self::$_instance;
    }

    public function dealInfo($data) {
        $kjjg_arr = str_split(trim($data['kjjg']),2);
        $code_arr = explode(',',$data['tzhm']);
        $kjjg_1 = $kjjg_arr[0];
        $kjjg_2 = $kjjg_arr[1];
        $kjjg_3 = $kjjg_arr[2];
        $kjjg_4 = $kjjg_arr[3];
        $kjjg_5 = $kjjg_arr[4];
        $kjjg_6 = $kjjg_arr[5];
        $kjjg_7 = $kjjg_arr[6];
        $kjjg_8 = $kjjg_arr[7];
        $kjjg_9 = $kjjg_arr[8];
        $kjjg_10 = $kjjg_arr[9];
        $zjzs = 0;
        $zjje = 0;
		$fdzs = 0;
	try{
        switch($data['wfid']) {
        	case '121':
        		if(strpos($data['tzhm'],$kjjg_1)!== false){
        			$zjzs=$zjzs+1;
        		}
        	    break;
        	case '122':
        		if(strpos($code_arr[0],$kjjg_1)!== false){
        			if(strpos($code_arr[1],$kjjg_2)!== false){
        				$zjzs=$zjzs+1;
        			}
        		}
        	    break;
        	 case '123':
        	 	if(strpos($code_arr[0],$kjjg_1)!== false){
        	 		if(strpos($code_arr[1],$kjjg_2)!== false){
        	 			if(strpos($code_arr[2],$kjjg_3)!== false){
        	 				$zjzs=$zjzs+1;
        	 			}
        	 		}
        	 	}	
        	    break;
        	 case '124':
        	 	if(strpos($code_arr[0],$kjjg_1)!== false){
        	 		$zjzs=$zjzs+1;
        	 	}
        	 	if(strpos($code_arr[1],$kjjg_2)!== false){
        	 		$zjzs=$zjzs+1;
        	 	}
        	 	if(strpos($code_arr[2],$kjjg_3)!== false){
        	 		$zjzs=$zjzs+1;
        	 	}
        	 	if(strpos($code_arr[3],$kjjg_4)!== false){
        	 		$zjzs=$zjzs+1;
        	 	}
        	 	if(strpos($code_arr[4],$kjjg_5)!== false){
        	 		$zjzs=$zjzs+1;
        	 	}		
        	    break;
        	 case '125':
        	 	if(strpos($code_arr[0],$kjjg_6)!== false){
        	 		$zjzs=$zjzs+1;
        	 	}
        	 	if(strpos($code_arr[1],$kjjg_7)!== false){
        	 		$zjzs=$zjzs+1;
        	 	}
        	 	if(strpos($code_arr[2],$kjjg_8)!== false){
        	 		$zjzs=$zjzs+1;
        	 	}
        	 	if(strpos($code_arr[3],$kjjg_9)!== false){
        	 		$zjzs=$zjzs+1;
        	 	}
        	 	if(strpos($code_arr[4],$kjjg_10)!== false){
        	 		$zjzs=$zjzs+1;
        	 	}
        	 	break;
        	 case '126':
        	 	$kjjg_h = $kjjg_1+$kjjg_2;
        	 	if($data['tzhm'] == '3' || $data['tzhm'] == '4' || $data['tzhm'] == '5' || $data['tzhm'] == '6' || $data['tzhm'] == '7' || $data['tzhm'] == '8' || $data['tzhm'] == '9' || $data['tzhm'] == '10' || $data['tzhm'] == '11' || $data['tzhm'] == '12' || $data['tzhm'] == '13' || $data['tzhm'] == '14' || $data['tzhm'] == '15' || $data['tzhm'] == '16' || $data['tzhm'] == '17' || $data['tzhm'] == '18' || $data['tzhm'] == '19'){
        	 		if($data['tzhm'] == $kjjg_h){ $zjzs += 1;}
        	 	}
        	 	break;
        	 case '127':
        	 	$code_arr=explode(" ",$data['tzhm']);
        	 	foreach ($code_arr as $v){
        	 		if($v=="龙" && $kjjg_1>$kjjg_10){
        	 			$zjzs=$zjzs+1;
        	 		}
        	 		if($v=="虎" && $kjjg_1<$kjjg_10){
        	 			$zjzs=$zjzs+1;
        	 		}
        	 	}
        	 	break;
        	 case '128':
        	 	$code_arr=explode(" ",$data['tzhm']);
        	 	foreach ($code_arr as $v){
        	 		if($v=="龙" && $kjjg_2>$kjjg_9){
        	 			$zjzs=$zjzs+1;
        	 		}
        	 		if($v=="虎" && $kjjg_2<$kjjg_9){
        	 			$zjzs=$zjzs+1;
        	 		}
        	 	}
        	 	break;
        	 case '129':
        	 	$code_arr=explode(" ",$data['tzhm']);
        	 	foreach ($code_arr as $v){
        	 		if($v=="龙" && $kjjg_3>$kjjg_8){
        	 			$zjzs=$zjzs+1;
        	 		}
        	 		if($v=="虎" && $kjjg_3<$kjjg_8){
        	 			$zjzs=$zjzs+1;
        	 		}
        	 	}
        	 	break;
        	 case '130':
        	 	$code_arr=explode(" ",$data['tzhm']);
        	 	foreach ($code_arr as $v){
        	 		if($v=="龙" && $kjjg_4>$kjjg_7){
        	 			$zjzs=$zjzs+1;
        	 		}
        	 		if($v=="虎" && $kjjg_4<$kjjg_7){
        	 			$zjzs=$zjzs+1;
        	 		}
        	 	}
        	 	break;
        	 case '131':
        	 	$code_arr=explode(" ",$data['tzhm']);
        	 	foreach ($code_arr as $v){
        	 		if($v=="龙" && $kjjg_5>$kjjg_6){
        	 			$zjzs=$zjzs+1;
        	 		}
        	 		if($v=="虎" && $kjjg_5<$kjjg_6){
        	 			$zjzs=$zjzs+1;
        	 		}
        	 	}
        	 	break;
        	 case '132':
        		if($kjjg_1=='1' || $kjjg_1 == '2'){
        	 		$code = explode(' ', $data['tzhm']);
        	 		foreach ($code as $it){
        	 			if($it=='金'){ $zjzs += 1;}
        	 		}
        	 	}
        	 	if($kjjg_1=='3' || $kjjg_1 == '4'){
        	 		$code = explode(' ', $data['tzhm']);
        	 		foreach ($code as $it){
        	 			if($it=='木'){ $zjzs += 1;}
        	 		}
        	 	}
        	 	if($kjjg_1=='5' || $kjjg_1 == '6'){
        	 		$code = explode(' ', $data['tzhm']);
        	 		foreach ($code as $it){
        	 			if($it=='水'){ $zjzs += 1;}
        	 		}
        	 	}
        	 	if($kjjg_1=='7' || $kjjg_1 == '8'){
        	 		$code = explode(' ', $data['tzhm']);
        	 		foreach ($code as $it){
        	 			if($it=='火'){ $zjzs += 1;}
        	 		}
        	 	}
        	 	if($kjjg_1=='9' || $kjjg_1 == '10'){
        	 		$code = explode(' ', $data['tzhm']);
        	 		foreach ($code as $it){
        	 			if($it=='土'){ $zjzs += 1;}
        	 		}
        	 	}	 
        	 	break;
        	 case '133':
        		if($kjjg_2=='1' || $kjjg_2 == '2'){
        	 		$code = explode(' ', $data['tzhm']);
        	 		foreach ($code as $it){
        	 			if($it=='金'){ $zjzs += 1;}
        	 		}
        	 		/* if(strstr($data['tzhm'], '金') !== false){
        	 			$zjzs += 1;
        	 		} */
        	 	}
        	 	if($kjjg_2=='3' || $kjjg_2 == '4'){
        	 		$code = explode(' ', $data['tzhm']);
        	 		foreach ($code as $it){
        	 			if($it=='木'){ $zjzs += 1;}
        	 		}
        	 	}
        	 	if($kjjg_2=='5' || $kjjg_2 == '6'){
        	 		$code = explode(' ', $data['tzhm']);
        	 		foreach ($code as $it){
        	 			if($it=='水'){ $zjzs += 1;}
        	 		}
        	 	}
        	 	if($kjjg_2=='7' || $kjjg_2 == '8'){
        	 		$code = explode(' ', $data['tzhm']);
        	 		foreach ($code as $it){
        	 			if($it=='火'){ $zjzs += 1;}
        	 		}
        	 	}
        	 	if($kjjg_2=='9' || $kjjg_2 == '10'){
        	 		$code = explode(' ', $data['tzhm']);
        	 		foreach ($code as $it){
        	 			if($it=='土'){ $zjzs += 1;}
        	 		}
        	 	}	 
        	 	break;
        	 case '134':
        	 	if($kjjg_3=='1' || $kjjg_3 == '2'){
        	 		$code = explode(' ', $data['tzhm']);
        	 		foreach ($code as $it){
        	 			if($it=='金'){ $zjzs += 1;}
        	 		}
        	 		/* if(strstr($data['tzhm'], '金') !== false){
        	 			$zjzs += 1;
        	 		} */
        	 	}
        	 	if($kjjg_3=='3' || $kjjg_3 == '4'){
        	 		$code = explode(' ', $data['tzhm']);
        	 		foreach ($code as $it){
        	 			if($it=='木'){ $zjzs += 1;}
        	 		}
        	 	}
        	 	if($kjjg_3=='5' || $kjjg_3 == '6'){
        	 		$code = explode(' ', $data['tzhm']);
        	 		foreach ($code as $it){
        	 			if($it=='水'){ $zjzs += 1;}
        	 		}
        	 	}
        	 	if($kjjg_3=='7' || $kjjg_3 == '8'){
        	 		$code = explode(' ', $data['tzhm']);
        	 		foreach ($code as $it){
        	 			if($it=='火'){ $zjzs += 1;}
        	 		}
        	 	}
        	 	if($kjjg_3=='9' || $kjjg_3 == '10'){
        	 		$code = explode(' ', $data['tzhm']);
        	 		foreach ($code as $it){
        	 			if($it=='土'){ $zjzs += 1;}
        	 		}
        	 	}	 
        	 	break;
        	 case '135':
        	 	if($kjjg_1 > 5) {
        	 		$sign_1 = '大';
        	 	} else {
        	 		$sign_1 = '小';
        	 	}
        	 	$code = explode(' ', $data['tzhm']);
        	 	foreach ($code as $it){
        	 		if($it==$sign_1){ $zjzs = 1;}
        	 	}
        	 	break;
        	 case '136':
        	 	if($kjjg_2 > 5) {
        	 		$sign_1 = '大';
        	 	} else {
        	 		$sign_1 = '小';
        	 	}
        		$code = explode(' ', $data['tzhm']);
        	 	foreach ($code as $it){
        	 		if($it==$sign_1){ $zjzs = 1;}
        	 	}
        	 	break;
        	 case '137':
        	 	if($kjjg_3 > 5) {
        	 		$sign_1 = '大';
        	 	} else {
        	 		$sign_1 = '小';
        	 	}
        	 	$code = explode(' ', $data['tzhm']);
        	 	foreach ($code as $it){
        	 		if($it==$sign_1){ $zjzs = 1;}
        	 	}
        	 	//if(mb_strpos($data['tzhm'],$sign_1) !== false){ $zjzs = 1;}
        	 	break;
        	 case '138':
        	 	if(($kjjg_1%2) == 0) {
        	 		$sign_2 = '双';
        	 	} else {
        	 		$sign_2 = '单';
        	 	}
        	 	$code = explode(' ', $data['tzhm']);
        	 	foreach ($code as $it){
        	 		if($it==$sign_2){ $zjzs = 1;}
        	 	}
        	 	//if(mb_strpos($data['tzhm'],$sign_2) !== false){ $zjzs = 1;}
        	 	break;
        	 case '139':
        	 	if(($kjjg_2%2) == 0) {
        	 		$sign_2 = '双';
        	 	} else {
        	 		$sign_2 = '单';
        	 	}
        	 	$code = explode(' ', $data['tzhm']);
        	 	foreach ($code as $it){
        	 		if($it==$sign_2){ $zjzs = 1;}
        	 	}
        	 	//if(mb_strpos($data['tzhm'],$sign_2) !== false){ $zjzs = 1;}
        	 	break;
        	 case '140':
        	 	if(($kjjg_3%2) == 0) {
        	 		$sign_2 = '双';
        	 	} else {
        	 		$sign_2 = '单';
        	 	}
        	 	$code = explode(' ', $data['tzhm']);
        	 	foreach ($code as $it){
        	 		if($it==$sign_2){ $zjzs = 1;}
        	 	}
        	 	//if(mb_strpos($data['tzhm'],$sign_2) !== false){ $zjzs = 1;}
        	 	break;
        	 case '141':
        	 	$code_arr=explode(" ",trim($data['tzhm']));
        	 	$gyhezhi = intval($kjjg_1)+intval($kjjg_2);
        	 	foreach ($code_arr as $v){
					$v = trim($v);
        	 		if($v=="大" && $gyhezhi>11){
        	 			$zjzs=$zjzs+1;
        	 		}
        	 		if($v=="小" && $gyhezhi<11){
        	 			$zjzs=$zjzs+1;
        	 		}
					if($v=="和" && 11==$gyhezhi){
						$zjzs=$zjzs+1;
					}
        	 		if($v=="单" && $gyhezhi!=11 && $gyhezhi%2==1){
        	 			$zjzs=$zjzs+1;
        	 		}
        	 		if($v=="双" && $gyhezhi!=11 && $gyhezhi%2==0){
        	 			$zjzs=$zjzs+1;
        	 		}
					if( $v!="和" && 11==$gyhezhi ){
						$fdzs += 1;
					}
        	 	}
        	 	break;
			case '292':
				foreach($code_arr as $k=>$v){
					$position = intval($k) + 1;
					$code = 'kjjg_'.$position;
					$temp = 0;
					if( $position > 5 ){
						$code_tmp = 'temp';
					}else{
						$code_tmp = 'kjjg_'.(11-$position);
					}
					$kjjg_format = self::format_tzhm($$code,$position,$$code_tmp);
					$val = explode(' ',trim($v));
					foreach($val as $value){
						!empty($value) && in_array($value,$kjjg_format) && $zjzs += 1;
					}
				}
				break;
			case '293':
				if(strpos($code_arr[0],$kjjg_1)!== false){
					$zjzs=$zjzs+1;
				}
				if(strpos($code_arr[1],$kjjg_2)!== false){
					$zjzs=$zjzs+1;
				}
				if(strpos($code_arr[2],$kjjg_3)!== false){
					$zjzs=$zjzs+1;
				}
				if(strpos($code_arr[3],$kjjg_4)!== false){
					$zjzs=$zjzs+1;
				}
				if(strpos($code_arr[4],$kjjg_5)!== false){
					$zjzs=$zjzs+1;
				}
				if(strpos($code_arr[5],$kjjg_6)!== false){
					$zjzs=$zjzs+1;
				}
				if(strpos($code_arr[6],$kjjg_7)!== false){
					$zjzs=$zjzs+1;
				}
				if(strpos($code_arr[7],$kjjg_8)!== false){
					$zjzs=$zjzs+1;
				}
				if(strpos($code_arr[8],$kjjg_9)!== false){
					$zjzs=$zjzs+1;
				}
				if(strpos($code_arr[9],$kjjg_10)!== false){
					$zjzs=$zjzs+1;
				}
				break;
			case '299':
				$code_arr = explode(' ',trim($data['tzhm']));
				$pl_arr = explode( '|',trim($data['dqpl']) );
				$sum = intval($kjjg_1) + intval($kjjg_2);
				foreach( $code_arr as $k=>$v ){
					$status = self::format_gyh($sum,trim($v));
					if( -1==$status ){
						$fdzs += 1;
					}elseif( 1==$status ){
						$zjzs += 1;
						$zjje += (floatval($data['dzje']) * floatval($pl_arr[$k]));
					}
				}
				break;
        }
	}catch (Exception $e){
		echo $e->getMessage() ;
		return false;
	}
        //结算中奖并转移结算数据
        if($data['nb']==1 || $data['swzh']==1) {
            $table = 'touzhu_list_sw';
        } else {
            $table = 'touzhu_list_ok';
        }

        //选择投注模式
        if($data['nbzh'] == 1) $pieces = 1;
        if($data['nbzh'] == 2) $pieces = 10;
        if($data['nbzh'] == 3) $pieces = 100;
        
        //处理中奖金额（赔率*单注金额*中奖注数/投注模式），特殊玩法除外

		if( 299==intval($data['wfid']) ){
			$zjje = $zjje / $pieces;
		}else{
			$zjje = $data['dqpl'] * $data['dzje'] * $zjzs / $pieces;
		}

		//处理返点金额
		$fdje = floatval($data['dzje']) * $fdzs / $pieces;
        //转移投注注单
        $insert_data = $data;
		$is_zh = $insert_data['from'];

        unset($insert_data['user_id'],$insert_data['swzh'],$insert_data['nb'],$insert_data['rktime'],$insert_data['cp_name_old'],$insert_data['cp_name_new'],$insert_data['from']);
        $insert_data['ifqs'] = '1';
        $insert_data['zjje'] = $zjje;
        $insert_data['zjzs'] = $zjzs;
		$insert_data['fdje'] += $fdje;
        
        $insert_pc = $insert_data;
        unset($insert_pc['tzhje'],$insert_pc['tz_time']);
        $tzhje = Db::name('username')->where('id',$data['user_id'])->value('je');
        $insert_pc['tzhje'] = $tzhje;
        $insert_pc['tz_time'] = time();
        
        Db::startTrans();
        try {
        	if(!Db::name($table)->data($insert_data)->insert()){
        		throw new \Exception(false);
        	}
        	if(!Db::name('touzhu_list_no')->where('id',$insert_data['id'])->delete()){
        		throw new \Exception(false);
        	}
			if( $fdje > 0 ){
                if(!Db::name('username')->where('id',$data['user_id'])->update(['je'=>['exp',"je+{$fdje}"]]) ){
                    throw new \Exception(false);
                } 
			}
        	//中奖金额大于0.001则给用户增加金额
        	if($zjje > 0.001) {
        		if(!Db::name('touzhu_list_pc')->data($insert_pc)->insert()){
        			throw new \Exception(false);
        		}
        		//$prefix = config('database.prefix');
        		//Db::execute("UPDATE {$prefix}username SET  je=je+'{$zjje}',zjzje=zjzje+'{$zjje}' WHERE id={$data['user_id']}");
        		if(!Db::name('username')->where('id',$data['user_id'])->update(['je'=>['exp',"je+{$zjje}"],'zjzje' =>['exp',"zjzje+{$zjje}"]])){
        			throw new \Exception(false);
        		}
				//处理追号自动停止
				if( 1==intval($is_zh) ){
					$list_zh = Db::name('touzhu_list_zh')->field('*,1 as type')->where(['qihao'=>['>',$data['qihao']]])->where(['czid'=>$data['czid'],'wfid'=>$data['wfid'],'stop'=>1])->select();
					$list_no = Db::name('touzhu_list_no')->field('*,2 as type')->where(['qihao'=>['>',$data['qihao']]])->where(['czid'=>$data['czid'],'wfid'=>$data['wfid'],'from'=>1])->select();
					$list = array_merge($list_zh,$list_no);
					$tkqje = Db::name('username')->where('id',$data['user_id'])->value('je');
                    //$tkqje = floatval($tzhje) + floatval($zjje);
					foreach($list as $v){
						$id = $v['id'];
						unset($v['id']);
						if( 1==$v['type'] ){
							unset($v['type']);
							unset($v['stop']);
							if( !Db::name('touzhu_list_zh')->delete($id) ){
								throw new \Exception(false);
							}
						}else{
							unset($v['rktime']);
							unset($v['type']);
							unset($v['from']);
							if( !Db::name('touzhu_list_no')->delete($id) ){
								throw new \Exception(false);
							}
						}
						$v['tktime'] = time();
						$v['tkqje'] = $tkqje;
						$tkqje += floatval($v['tzje']);

						if( !Db::name('touzhu_list_tk')->insert($v) ){
							throw new \Exception(false);
						}
						if( !Db::name('username')->where('id',$data['user_id'])->update(array('je'=>['exp',"je+'{$v['tzje']}'"],'tzzje'=>['exp',"tzzje-'{$v['tzje']}'"])) ){
							throw new \Exception(false);
						}
					}
				}
        	}
            /* Db::name($table)->data($insert_data)->insert();
            Db::name('touzhu_list_no')->where('id',$insert_data['id'])->delete();
            //中奖金额大于0.001则给用户增加金额
            if($zjje > 0.001) {
               // $prefix = config('database.prefix');
                //Db::execute("UPDATE {$prefix}username SET  je=je+'{$zjje}',zjzje=zjzje+'{$zjje}' WHERE id={$data['user_id']}");
            	Db::name('username')->where('id',$data['user_id'])->update(['je'=>['exp',"je+{$zjje}"],'zjzje' =>['exp',"zjzje+{$zjje}"]]);
            } */
            Db::commit();
            return $zjje;
        } catch (\Exception $e) {
            Db::rollback();
            return false;
        }

    }
	private static function format_tzhm($code,$position,$code2_tmp=0){
		$ret = [];
		$ret[] = intval($code) < 6 ? '小' : '大';
		$ret[] = 0==intval($code)%2 ? '双' : '单';
		if( $position < 6 ){
			$ret[] = intval($code) > intval($code2_tmp) ? '龙' : '虎';
		}
		return $ret;
	}
	private static function format_gyh($sum,$code){
		//-1:返还，0:未中奖，1:中奖
		$win = 0;
		if($code=="大" && $sum>11){
			$win = 1;
		}
		if($code=="小" && $sum<11){
			$win = 1;
		}
		if($code=="和" && 11==$sum){
			$win = 1;
		}
		if($code=="单" && $sum!=11 && $sum%2==1){
			$win = 1;
		}
		if($code=="双" && $sum!=11 && $sum%2==0){
			$win = 1;
		}
		if( $code!="和" && !is_numeric($code) && 11==$sum ){
			$win = -1;
		}
		if( is_numeric($code) && $sum==$code ){
			$win = 1;
		}
		return $win;
	}
}